package com.example.grape_effect.effet.effect

import com.example.grape_effect.gl.common.ShaderParameter


class WaveEffect: LYEffect() {
    override var fragmentShader = """
    varying highp vec2 textureCoordinate;
    uniform sampler2D inputTexture;
//const float _distanceFactor = 0.1;
    uniform highp float time;
    uniform float screenWidth;
    uniform float screenHeight;
    void main(){

        //计算uv到中间点的向量(向外扩，反过来就是向里缩)... 这里如果不加highp 会发现中间区域是0，导致纹理采样是一块像素
        highp vec2 dv = vec2(0.5, 0.5) - textureCoordinate;
        // 利用屏幕比 来使得波纹是园的
        dv = dv * vec2(screenWidth /screenHeight, 1.0);
        //    vec2 dv = textureCoordinate - vec2(0.5, 0.5);
        float dis = sqrt(dv.x * dv.x + dv.y * dv.y);

        highp float sinFactor = sin(dis * 60.0   + time /1500.0 * -30.0) * 5.0 * 0.01;//500 消失，那么500内走多少个波，1500 波的周期T
        highp vec2 dv1 = normalize(dv);
        //计算每个像素uv的偏移值
        //        vec2 offset = dv1  * sinFactor/ 1080.0 * 100.0;

        // 0.3 是波长，  0.32 是波速
        highp float discardFactor = clamp(0.3 - abs(0.32 * time/500.0 - dis), 0.0, 1.0);// 500 控制消失
        //像素采样时偏移offset
        vec2 offset = dv1 * sinFactor;
        highp vec2 uv =  textureCoordinate + offset * discardFactor;//* discardFactor
        gl_FragColor = texture2D(inputTexture, uv);
    }
    """.trimIndent()
    init {
        name = "waveEffect"
    }
    override fun addParameters() {
        super.addParameters()
        parameters["screenWidth"] = ShaderParameter(
            "screenWidth",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
        parameters["screenHeight"] = ShaderParameter(
            "screenHeight",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
        parameters["time"] = ShaderParameter(
            "time",
            ShaderParameter.TYPE_UNIFORM,
            ShaderParameter.VALUE_TYPE_FLOAT
        )
    }

    override fun setDefaultParameterValues() {
        super.setDefaultParameterValues()
        parameters["screenWidth"]?.value = 675f
        parameters["screenHeight"]?.value = 1200f
    }


    override fun draw() {
        if( parameters["screenWidth"]?.value ==0) {
//            parameters["screenWidth"]?.value = getInTexture().width
//            parameters["screenHeight"]?.value = getInTexture().height
        }
        // 2s 一个水花
        parameters["time"]?.value = ((System.currentTimeMillis() - startTime)%2000).toFloat()
        super.draw()
    }

}
/***参考：http://blog.csdn.net/puppet_master/article/details/52975666
uniform sampler2D _MainTex;
uniform float _distanceFactor;
uniform float _timeFactor;
uniform float _totalFactor;
uniform float _waveWidth;
uniform float _curWaveDis;

fixed4 frag(v2f_img i) : SV_Target
{
//计算uv到中间点的向量(向外扩，反过来就是向里缩)
    float2 dv = float2(0.5, 0.5) - i.uv;
//按照屏幕长宽比进行缩放
    dv = dv * float2(_ScreenParams.x / _ScreenParams.y, 1);
//计算像素点距中点的距离
    float dis = sqrt(dv.x * dv.x + dv.y * dv.y);
//用sin函数计算出波形的偏移值factor
//dis在这里都是小于1的，所以我们需要乘以一个比较大的数，比如60，这样就有多个波峰波谷
//sin函数是（-1，1）的值域，我们希望偏移值很小，所以这里我们缩小100倍，据说乘法比较快,so...
    float sinFactor = sin(dis * _distanceFactor + _Time.y * _timeFactor) * _totalFactor * 0.01;
//距离当前波纹运动点的距离，如果小于waveWidth才予以保留，否则已经出了波纹范围，factor通过clamp设置为0
    float discardFactor = clamp(_waveWidth - abs(_curWaveDis - dis), 0, 1);
//归一化
    float2 dv1 = normalize(dv);
//计算每个像素uv的偏移值
    float2 offset = dv1  * sinFactor * discardFactor;
//像素采样时偏移offset
    float2 uv = offset + i.uv;
    return tex2D(_MainTex, uv);
}

using UnityEngine;

public class WaterWaveEffect : PostEffectBase {


//距离系数
    public float distanceFactor = 60.0f;
//时间系数
    public float timeFactor = -30.0f;
//sin函数结果系数
    public float totalFactor = 1.0f;

//波纹宽度
    public float waveWidth = 0.3f;
//波纹扩散的速度
    public float waveSpeed = 0.3f;

    private float waveStartTime;

    void OnRenderImage (RenderTexture source, RenderTexture destination)
{
    //计算波纹移动的距离，根据enable到目前的时间*速度求解
    float curWaveDistance = (Time.time - waveStartTime) * waveSpeed;
//设置一系列参数
    _Material.SetFloat("_distanceFactor", distanceFactor);
    _Material.SetFloat("_timeFactor", timeFactor);
    _Material.SetFloat("_totalFactor", totalFactor);
    _Material.SetFloat("_waveWidth", waveWidth);
    _Material.SetFloat("_curWaveDis", curWaveDistance);

    Graphics.Blit (source, destination, _Material);
}

void OnEnable()
{
    //设置startTime
    waveStartTime = Time.time;
}
}
***参考：http://blog.csdn.net/puppet_master/article/details/52975666 **/